﻿''' <summary>
''' This class implements a writer that generates a text file.
''' </summary>
''' <remarks>
''' The Text writer creates a simple text output file.
''' </remarks>
Public Class TextWriter
    Implements IWriter

    ''' <summary>
    ''' Stream writer for text output.
    ''' </summary>
    ''' <remarks></remarks>
    Protected SW As System.IO.StreamWriter

    ''' <summary>
    ''' Output file.
    ''' </summary>
    ''' <remarks></remarks>
    Protected fName As String

    Public Sub Cleanup() Implements IWriter.Cleanup
        'Close the output file.
        SW.Close()
        SW = Nothing
    End Sub

    Public Sub Startup(ByVal cfgFile As String) Implements IWriter.Startup

        'Read the configuration from the XML file.
        Dim ds As New DataSet
        ds.ReadXml(cfgFile)

        'Get the file to output.
        fName = Convert.ToString(ds.Tables(0).Rows(0).Item("ReportOutput"))

        'Perform replacements.
        fName = fName.Replace("$YYYY$", Now.Year.ToString())
        fName = fName.Replace("$MM$", Now.Month.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$DD$", Now.Day.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$HH$", Now.Hour.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$mm$", Now.Minute.ToString().PadLeft(2, "0"c))
        fName = fName.Replace("$SS$", Now.Second.ToString().PadLeft(2, "0"c))

        'Open the file.
        SW = New System.IO.StreamWriter(fName, False, System.Text.Encoding.Default)

        ds.Dispose()
        ds = Nothing

    End Sub

    Public Sub WriteBlank() Implements IWriter.WriteBlank
        SW.WriteLine()
    End Sub

    Public Sub WriteBlankEntry() Implements IWriter.WriteBlankEntry
        SW.WriteLine()
    End Sub

    Public Sub WriteCategory(ByVal str As String) Implements IWriter.WriteCategory
        SW.WriteLine(str)
        SW.WriteLine(New String("-"c, str.Length()))
        WriteBlank()
    End Sub

    Public Sub WriteCategoryError(ByVal str As String) Implements IWriter.WriteCategoryError
        SW.WriteLine("*** " + str)
    End Sub

    Public Sub WriteEntry(ByVal str As String) Implements IWriter.WriteEntry
        SW.WriteLine("   " + str)
    End Sub

    Public Sub WriteEntry(ByVal id As String, ByVal value As String) Implements IWriter.WriteEntry
        SW.WriteLine("   " + id.PadRight(25, " "c) + "      " + value)
    End Sub

    Public Sub WriteEntryError(ByVal str As String) Implements IWriter.WriteEntryError
        SW.WriteLine("   *** " + str)
    End Sub

    Public Sub WriteFooter() Implements IWriter.WriteFooter
        SW.WriteLine(New String("-"c, 40))
    End Sub

    Public Sub WritePageBreak() Implements IWriter.WritePageBreak
        For i As Integer = 1 To 25
            WriteBlank()
        Next
    End Sub

    Public Sub WriteSection(ByVal str As String) Implements IWriter.WriteSection
        SW.WriteLine(str)
        SW.WriteLine(New String("="c, str.Length()))
        WriteBlank()
    End Sub

    Public Sub WriteSectionError(ByVal str As String) Implements IWriter.WriteSectionError
        SW.WriteLine("*** " + str)
    End Sub

    Public Sub WriteSystem(ByVal str As String) Implements IWriter.WriteSystem
        SW.WriteLine(str)
        WriteBlank()
    End Sub

    Public Sub WriteTitle(ByVal str As String) Implements IWriter.WriteTitle
        SW.WriteLine(" " + str)
        SW.WriteLine(New String("@"c, str.Length + 2))
        WriteBlank()
    End Sub

    Public Sub WriteVersion(ByVal str As String) Implements IWriter.WriteVersion
        SW.WriteLine("v." + str)
        WriteBlank()
    End Sub

    Public Function GetOutputFile() As String Implements IWriter.GetOutputFile
        Return fName
    End Function

End Class
